package com.niodata.dp.task;

import com.niodata.dp.task.entity.v2.PackageParamsV2.PackageParamGroup;
import com.niodata.dp.task.entity.v2.PackageParamsV2.PackageParamV2;
import com.niodata.dp.util.JsonUtil;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.commons.io.IOUtils;
import org.apache.logging.log4j.util.Strings;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

public class JobTypeParamSchemasV2 {

  private static Map<String, PackageParamGroup> paramSchemas = new HashMap<>();
  private static Set<String> jobTypeInternalParams = new HashSet<String>();

  /**
   * load job type param schemas from class path.
   */
  static {
    String[] internalParams = new String[]{"type", "keytab", "principal",
        "dependencies", "working.dir", "name"};
    for (String p : internalParams) {
      jobTypeInternalParams.add(p);
    }
    PathMatchingResourcePatternResolver resourcePatternResolver =
        new PathMatchingResourcePatternResolver();
    try {
      Resource[] resources = resourcePatternResolver
          .getResources("classpath:META-INF/param.schemas/v2/*");
      for (Resource res : resources) {
        String fileName = res.getFilename();
        String[] ss = fileName.split("/");
        String name = ss[ss.length - 1];
        name = name.split("\\.")[0];
        InputStream inputStream = res.getInputStream();
        String json = new String(IOUtils.toByteArray(inputStream));
        List<PackageParamV2> schemas = JsonUtil.fromJsonList(PackageParamV2.class, json);
        PackageParamGroup group = new PackageParamGroup();
        group.relatedParams = schemas;
        group.description = "插件运行时基础配置";
        group.name = "运行环境配置";
        paramSchemas.put(name, group);
        inputStream.close();
      }
    } catch (IOException ioe) {
      throw new RuntimeException(ioe);
    }
  }

  private JobTypeParamSchemasV2() {
  }

  public static PackageParamGroup getJobTypeParamSchemas(String jobtype, String language) {
    /*
    String key = jobtype + "-" + language;
    if (jobtype.equals("command")) {
      key = jobtype;
    }
    */
    String key = jobtype;
    if (jobtype.equals("spark")) {
      key = jobtype + "-" + language;
    }
    return paramSchemas.get(key);
  }

  public static boolean isUserSetAble(String jobType, String language, String name) {
    PackageParamGroup group = getJobTypeParamSchemas(jobType, language);
    for (PackageParamV2 v2:group.relatedParams) {
      if (name.equals(v2.name)) {
        return v2.userSetAble;
      }
    }
    return false;
  }

  public static Map<String, String> getJobTypeDefaultParams(String jobtype, String language) {
    PackageParamGroup group = getJobTypeParamSchemas(jobtype, language);
    Map<String, String> map = new HashMap<>();
    for (PackageParamV2 v2:group.relatedParams) {
      if (!Strings.isBlank(v2.defaultValue)) {
        map.put(v2.name, v2.defaultValue);
      }
    }
    return map;
  }

  public static boolean isInternalParam(String name) {
    return jobTypeInternalParams.contains(name);
  }

}
